In this week's group work we had to build a machine that included a mechaincal design and automation. We went with a messaging device called FABulous massager
We used an Arduino UNO and a CNC shield to control the motors. The CNC shield is a board that allows you to control stepper motors using an Arduino. It has slots for connecting stepper motor drivers, which are used to control the motors' movements. It used an an DRV 8825 driver. We used an external power supply to connect to the motors since they require 12V. I used the following website to learn more about the CNC shield and was really helpful in determining the pins: Page
We used two stepper motors in the device with both connected to a custom designed attachment so we could place the strings in there. Thereforce, once the motors moved there was enough friction for strings to properly move and move the massage holder.
We used a joystick to control the motors. The joystick is a two-axis input device that allows you to control the movement of the motors in both X and Y directions. Also, when you pressed the buttons the joystick would then move forward and back 200 steps athough I did add a debounce in the code so it didn't register the pressed too fast and overwhelm itself. Moreover, you could move both motors simultaneously if you had the joystick midway both directions.
I used the website to see the pins needed for the motors which really helped a lot
The code was written in the Arudino IDE.
The following code defines the motor pins and shows the use of accel library. With stepper motor 1 using pin 2 for step and 5 for direction and stepper motor 2 using pin 3 for step and 6 for direction.
#include
#define motorInterfaceType 1
AccelStepper stepper1(motorInterfaceType, 2, 5); // X Axis (left/right)
AccelStepper stepper2(motorInterfaceType, 3, 6); // Y Axis (forward/backward)
Joystick pins
const int joystickXPin = A0;
const int joystickYPin = A1;
const int joystickButtonPin = A2;
The following code makes the the motors move back and forth 200 steps by the press of a button. With one press causing them to move in one direction and then another press casuing it move in the opposite direction. The code also includes a debounce delay to prevent multiple presses from being registered too quickly.
if (buttonPressed && !buttonPreviouslyPressed && (now - lastButtonPress > debounceDelay)) {
lastButtonPress = now;
// Toggle direction: move forward or backward
if (moveForward) {
stepper1.move(moveChunk); // Move forward
stepper2.move(moveChunk); // Move forward
Serial.println("Moving both motors forward.");
} else {
stepper1.move(-moveChunk); // Move backward
stepper2.move(-moveChunk); // Move backward
Serial.println("Moving both motors backward.");
}
// Toggle the direction for next button press
moveForward = !moveForward;
currentMode = MOVING;
}
The following code shows how the joystick is used to control the motors. The joystick values are read and mapped to the motor speeds. The motors are then moved based on the joystick position. The code also includes a dead zone to prevent small joystick movements from causing motor movement.
// Manual joystick control
if (currentMode == MANUAL) {
int xVal = analogRead(joystickXPin);
int yVal = analogRead(joystickYPin);
int xOffset = xVal - joystickCenter;
int yOffset = yVal - joystickCenter;
if (abs(xOffset) > deadZone) {
stepper1.setSpeed(map(xOffset, -512, 511, -maxSpeed, maxSpeed));
} else {
stepper1.setSpeed(0);
}
if (abs(yOffset) > deadZone) {
stepper2.setSpeed(map(yOffset, -512, 511, maxSpeed / 3, -maxSpeed / 3));
} else {
stepper2.setSpeed(0);
}
stepper1.runSpeed();
stepper2.runSpeed();
}
In the end, we were able to create a device that could move in both directions and was controlled by a joystick. The device was able to move the massage holder back and forth and left and right. The device was also able to move both motors simultaneously. There was a lot to learn and I really enjoyed working on the device as I got to know a lot more about screws and how really important they are 😊. Though it was the small details that really took a lot of the time.